/**
 * file
 */
package com.tdkj.action.front.member.albumManagement;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanFile;
import com.tdkj.model.common.BeanPhotoCategory;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.common.IBeanPhotoCategoryService;
import com.tdkj.util.FileUtil;

public class PhotoCategoryAction extends BaseAction {
	private Long id;
	private Long pid;
	private String name;
	private String photo1;
	private String photo2;
	private String photo3;
	private String photo4;
	private String photo5;
	private String photo6;
	private BeanPhotoCategory category;
	private BeanFile photo;
	private List<BeanPhotoCategory> categorys;
	private IBeanPhotoCategoryService beanPhotoCategoryService;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getPid() {
		return pid;
	}
	public void setPid(Long pid) {
		this.pid = pid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhoto1() {
		return photo1;
	}
	public void setPhoto1(String photo1) {
		this.photo1 = photo1;
	}
	public String getPhoto2() {
		return photo2;
	}
	public void setPhoto2(String photo2) {
		this.photo2 = photo2;
	}
	public String getPhoto3() {
		return photo3;
	}
	public void setPhoto3(String photo3) {
		this.photo3 = photo3;
	}
	public String getPhoto4() {
		return photo4;
	}
	public void setPhoto4(String photo4) {
		this.photo4 = photo4;
	}
	public String getPhoto5() {
		return photo5;
	}
	public void setPhoto5(String photo5) {
		this.photo5 = photo5;
	}
	public String getPhoto6() {
		return photo6;
	}
	public void setPhoto6(String photo6) {
		this.photo6 = photo6;
	}
	public BeanPhotoCategory getCategory() {
		return category;
	}
	public void setCategory(BeanPhotoCategory category) {
		this.category = category;
	}
	public BeanFile getPhoto() {
		return photo;
	}
	public void setPhoto(BeanFile photo) {
		this.photo = photo;
	}
	public List<BeanPhotoCategory> getCategorys() {
		return categorys;
	}
	public void setCategorys(List<BeanPhotoCategory> categorys) {
		this.categorys = categorys;
	}
	@Resource
	public void setBeanPhotoCategoryService(
			IBeanPhotoCategoryService beanPhotoCategoryService) {
		this.beanPhotoCategoryService = beanPhotoCategoryService;
	}
	
	public String findList(){
		Criteria criteria = this.createCriteria(BeanPhotoCategory.class);
		//加限制条件，只处理用户相关
		criteria.add(Restrictions.eq("user",this.getCurrentUser()));
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "photoCategoryAction!findList.action", 10);
		List l=this.getPager().getList();
		if(l.size()==0){
			appendObj();
		}else{
			categorys=l;
		}
		return "list";
	}
	public String findAddPage(){
		return "addPage";
	}
	public String findPhotoAddPage(){
		categorys=beanPhotoCategoryService.findAll(BeanPhotoCategory.class);
		return "photoAddPage";
	}
	public String findEditPage(){
		category=beanPhotoCategoryService.findById(this.getId(), BeanPhotoCategory.class);
		return "editPage";
	}
	
	public String findPhotoList(){
		BeanPhotoCategory bean=beanPhotoCategoryService.findById(this.getId(), BeanPhotoCategory.class);
		Criteria criteria = this.createCriteria(BeanFile.class)
		.add(Restrictions.eq("photocategory", bean));
		//加限制条件，只处理用户相关
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "photoCategoryAction!findPhotoList.action", 10);
		return "photoList";
	}
	public String addCategory(){
		BeanUser u=this.getCurrentUser();
		BeanPhotoCategory bean=new BeanPhotoCategory();
		bean.setName(this.getName());
		bean.setUser(u);
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		beanPhotoCategoryService.save(bean);
		return findList();
	}
	public String addPhoto(){
		BeanPhotoCategory bean=beanPhotoCategoryService.findById(this.getId(), BeanPhotoCategory.class);
		bean.setStatus("未审核");
		BeanFile f1=beanPhotoCategoryService.findFileByUUID(this.getPhoto1());
		BeanFile f2=beanPhotoCategoryService.findFileByUUID(this.getPhoto2());
		BeanFile f3=beanPhotoCategoryService.findFileByUUID(this.getPhoto3());
		BeanFile f4=beanPhotoCategoryService.findFileByUUID(this.getPhoto4());
		BeanFile f5=beanPhotoCategoryService.findFileByUUID(this.getPhoto5());
		BeanFile f6=beanPhotoCategoryService.findFileByUUID(this.getPhoto6());
		if(f1!=null) {f1.setPhotocategory(bean);f1.setStatus("未审核"); beanPhotoCategoryService.saveOrUpdate(f1, BeanFile.class);}//bean.getPhotos().add(f1);
		if(f2!=null) {f2.setPhotocategory(bean);f2.setStatus("未审核"); beanPhotoCategoryService.saveOrUpdate(f2, BeanFile.class);}//bean.getPhotos().add(f2);
		if(f3!=null) {f3.setPhotocategory(bean);f3.setStatus("未审核"); beanPhotoCategoryService.saveOrUpdate(f3, BeanFile.class);}//bean.getPhotos().add(f3);
		if(f4!=null) {f4.setPhotocategory(bean);f4.setStatus("未审核"); beanPhotoCategoryService.saveOrUpdate(f4, BeanFile.class);}//bean.getPhotos().add(f4);
		if(f5!=null) {f5.setPhotocategory(bean);f5.setStatus("未审核"); beanPhotoCategoryService.saveOrUpdate(f5, BeanFile.class);}//bean.getPhotos().add(f5);
		if(f6!=null) {f6.setPhotocategory(bean);f6.setStatus("未审核"); beanPhotoCategoryService.saveOrUpdate(f6, BeanFile.class);}//bean.getPhotos().add(f6);
		return findPhotoAddPage();
	}
	public String editCategory(){
		BeanPhotoCategory bean=new BeanPhotoCategory();
		bean.setId(this.id);
		bean.setName(this.getName());
		beanPhotoCategoryService.saveOrUpdate(bean, BeanPhotoCategory.class);
		return findList();
	}
	public String deleteCategory(){
		beanPhotoCategoryService.delete(this.getId(), BeanPhotoCategory.class);
		return findList();
	}
	public String deletePhoto() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String path=request.getRealPath("");
		path=path.replaceAll("\\\\", "/");
		beanPhotoCategoryService.deletePhoto(this.getPid(),path);
		//id=this.getId();
		return findPhotoList();
	}
	private void appendObj(){
		BeanPhotoCategory bean=null;
		BeanUser u=this.getCurrentUser();
		for(int i=0;i<10;i++){
			bean=new BeanPhotoCategory();
			bean.setName("我的图片夹"+i);
			bean.setUser(u);
			bean.setStatus("未审核");
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			beanPhotoCategoryService.save(bean);
		}
	}
}
